//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SABR.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Interpolation.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Math::Optimization;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CSABR::CSABR (Double t, Double forward, Double alpha, Double beta, Double nu, Double rho, Boolean alphaIsFixed, Boolean betaIsFixed, Boolean nuIsFixed, Boolean rhoIsFixed, Microsoft::FSharp::Core::FSharpOption<Boolean>^ vegaWeighted, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>^ endCriteria, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>^ optMethod) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CEndCriteria^ _CendCriteria;
    COptimizationMethod^ _CoptMethod;
    try
    {
#ifdef HANDLE
        _phSABR = NULL;
#endif
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        QuantLib::Real _forward = (QuantLib::Real)ValueHelper::Convert (forward);
        QuantLib::Real _alpha = (QuantLib::Real)ValueHelper::Convert (alpha);
        QuantLib::Real _beta = (QuantLib::Real)ValueHelper::Convert (beta);
        QuantLib::Real _nu = (QuantLib::Real)ValueHelper::Convert (nu);
        QuantLib::Real _rho = (QuantLib::Real)ValueHelper::Convert (rho);
        bool _alphaIsFixed = (bool)ValueHelper::Convert (alphaIsFixed);
        bool _betaIsFixed = (bool)ValueHelper::Convert (betaIsFixed);
        bool _nuIsFixed = (bool)ValueHelper::Convert (nuIsFixed);
        bool _rhoIsFixed = (bool)ValueHelper::Convert (rhoIsFixed);
        bool _vegaWeighted = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (vegaWeighted) ? (bool)ValueHelper::Convert (vegaWeighted->Value) : false); //4
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>::IsSome::get (endCriteria))
        {
            _CendCriteria = safe_cast<CEndCriteria^> (endCriteria->Value);
            _CendCriteria->Lock();
        }
        boost::shared_ptr<QuantLib::EndCriteria> _endCriteria = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>::IsSome::get (endCriteria) ? static_cast<boost::shared_ptr<QuantLib::EndCriteria>> (_CendCriteria->GetShared ()) : boost::shared_ptr<QuantLib::EndCriteria>()); //1
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>::IsSome::get (optMethod))
        {
            _CoptMethod = safe_cast<COptimizationMethod^> (optMethod->Value);
            _CoptMethod->Lock();
        }
        boost::shared_ptr<QuantLib::OptimizationMethod> _optMethod = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>::IsSome::get (optMethod) ? static_cast<boost::shared_ptr<QuantLib::OptimizationMethod>> (_CoptMethod->GetShared ()) : boost::shared_ptr<QuantLib::OptimizationMethod>()); //1
        _ppSABR = new boost::shared_ptr<QuantLib::SABR> (new QuantLib::SABR ( _t,  _forward,  _alpha,  _beta,  _nu,  _rho,  _alphaIsFixed,  _betaIsFixed,  _nuIsFixed,  _rhoIsFixed,  _vegaWeighted,  _endCriteria,  _optMethod ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CendCriteria != nullptr) _CendCriteria->Unlock();
        if (_CoptMethod != nullptr) _CoptMethod->Unlock();
    }
}
Cephei::QL::Math::Interpolations::CSABR::CSABR (boost::shared_ptr<QuantLib::SABR>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSABR = NULL;
#endif
	_ppSABR = &childNative;
    
}
Cephei::QL::Math::Interpolations::CSABR::CSABR (QuantLib::SABR& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSABR = NULL;
#endif
	_ppSABR = new boost::shared_ptr<QuantLib::SABR> (&childNative);
    
    _SABROwner = owner;
    
}

Cephei::QL::Math::Interpolations::CSABR::CSABR (CSABR^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSABR = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSABR = new boost::shared_ptr<QuantLib::SABR> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Interpolations::CSABR::CSABR (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSABR = NULL;
#endif
	if (!t->IsSubclassOf(CSABR::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CSABR::CSABR (QuantLib::Handle<QuantLib::SABR>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSABR = &childNative;
	_ppSABR = &static_cast<boost::shared_ptr<QuantLib::SABR>>(childNative.currentLink());
    
    _SABROwner = owner;
}
Cephei::QL::Math::Interpolations::CSABR::CSABR (QuantLib::Handle<QuantLib::SABR> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSABR = &childNative;
	_ppSABR = &static_cast<boost::shared_ptr<QuantLib::SABR>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CSABR::CSABR (QuantLib::SABR childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSABR = NULL;
#endif
	_ppSABR = new boost::shared_ptr<QuantLib::SABR> (new QuantLib::SABR (childNative));
    
}
#endif

Cephei::QL::Math::Interpolations::CSABR::~CSABR ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSABR != NULL)
    {
	    delete _ppSABR;
        _ppSABR = NULL;
    }
}
Cephei::QL::Math::Interpolations::CSABR::!CSABR ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSABR != NULL)
    {
	    delete _ppSABR;
    }
}
QuantLib::SABR& Cephei::QL::Math::Interpolations::CSABR::GetReference ()
{
    if (_ppSABR == NULL) throw gcnew NativeNullException ();
	return **_ppSABR;
}
boost::shared_ptr<QuantLib::SABR>& Cephei::QL::Math::Interpolations::CSABR::GetShared ()
{
    if (_ppSABR == NULL) throw gcnew NativeNullException ();
	return *_ppSABR;
}
QuantLib::SABR* Cephei::QL::Math::Interpolations::CSABR::GetPointer ()
{
    if (_ppSABR == NULL) throw gcnew NativeNullException ();
	return &**_ppSABR;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SABR>& Cephei::QL::Math::Interpolations::CSABR::GetHandle ()
{
	if (_phSABR == NULL)
	{
		_phSABR = new Handle<QuantLib::SABR> (*_ppSABR);
	}
	return *_phSABR;
}
#endif
bool Cephei::QL::Math::Interpolations::CSABR::HasNative () 
{
	return (_ppSABR != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::ISABR^ Cephei::QL::Math::Interpolations::CSABR_Factory::Create (Double t, Double forward, Double alpha, Double beta, Double nu, Double rho, Boolean alphaIsFixed, Boolean betaIsFixed, Boolean nuIsFixed, Boolean rhoIsFixed, Microsoft::FSharp::Core::FSharpOption<Boolean>^ vegaWeighted, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IEndCriteria^>^ endCriteria, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::IOptimizationMethod^>^ optMethod)
{
    return gcnew CSABR ( t,  forward,  alpha,  beta,  nu,  rho,  alphaIsFixed,  betaIsFixed,  nuIsFixed,  rhoIsFixed,  vegaWeighted,  endCriteria,  optMethod);
}
